/*
 * @Author: worldzb 
 * @Date: 2018-12-03 22:47:32 
 * @Last Modified by:   worldzb 
 * @Last Modified time: 2018-12-03 22:47:32 
 */

export default class Hash{
    gethashcode() {
        //定义一个时间戳，计算与1970年相差的毫秒数  用来获得唯一时间
        let timestamp = (new Date()).valueOf();
        let myRandom=this.randomWord(true,6,20);
        let hashcode=this.hashCode(myRandom+timestamp.toString());
        return myRandom+timestamp.toString();
    }
    hashCode(str) {
        let h = 0;
        let len = str.length;
        let t = 2147483648;
        for (let i = 0; i < len; i++) {
            h = 31 * h + str.charCodeAt(i);
            if (h > 2147483647) h %= t; //java int溢出则取模
        }
        return h;
    }
    randomWord(randomFlag, min, max) {
        let str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
        // 随机产生
        if (randomFlag) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (let i = 0; i < range; i++) {
            let pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    }
}
